﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TraCuuTinRaoVat
{
    public partial class UserControlTaiKhoan : System.Web.UI.UserControl
    {
        static List<String> dsUserName = new List<string>();
        static List<String> dsEmail = new List<string>();
        static List<String> dsCMND = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
            var query = from Tin_Rao_Vat in danhsach.LOAI_NGUOI_DUNGs
                        select Tin_Rao_Vat.TenLoaiNguoiDung;

            dropLoaiNguoiDung.DataSource = query;
            dropLoaiNguoiDung.DataBind();

            LinQ_RaoVatDataContext danhsach2 = new LinQ_RaoVatDataContext();
            var query2 = from Tin_Rao_Vat in danhsach2.NGUOI_DUNGs
                        select Tin_Rao_Vat.TenDangNhap;

            foreach (String nd1 in query2)
            {
                dsUserName.Add(nd1.Trim());
            }


            LinQ_RaoVatDataContext danhsach3 = new LinQ_RaoVatDataContext();
            var query3 = from Tin_Rao_Vat in danhsach3.NGUOI_DUNGs
                         select Tin_Rao_Vat.Email;

            foreach (String nd2 in query3)
            {
                dsEmail.Add(nd2.Trim());
            }


            LinQ_RaoVatDataContext danhsach4 = new LinQ_RaoVatDataContext();
            var query4 = from Tin_Rao_Vat in danhsach4.NGUOI_DUNGs
                         select Tin_Rao_Vat.SoCMND;

            foreach (String nd3 in query4)
            {
                dsCMND.Add(nd3.Trim());
            }

            txtThoiGianDK.Text = DateTime.Now.ToShortDateString();
            txtThoiGianHetHan.Text = DateTime.Now.ToShortDateString();
            txtThoiGianHetHan_CalendarExtender.DaysModeTitleFormat = "dd-MM-yyyy";
            txtThoiGianDK_CalendarExtender.DaysModeTitleFormat = "dd-MM-yyyy";
            txtThoiGianHetHan_CalendarExtender.TodaysDateFormat = "dd-MM-yyyy";
            txtThoiGianDK_CalendarExtender.TodaysDateFormat = "dd-MM-yyyy";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                int temp = 1;
                for (int i = 0; i < dsUserName.Count; i++)
                {
                    if (dsUserName[i] == txtTenDN.Text)
                    {
                        temp = 0;
                    }

                }
                if ((temp == 1) && (txtTenDN.Text.Trim() != ""))
                {
                    LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
                    NGUOI_DUNG nguoiDung = new NGUOI_DUNG();
                    int loaiNguoiDung = dropLoaiNguoiDung.SelectedIndex + 1;
                    nguoiDung.MaLoaiNguoiDung = loaiNguoiDung;
                    nguoiDung.TenNguoiDung = txtTenNguoiDung.Text;
                    nguoiDung.TenDangNhap = txtTenDN.Text;
                    nguoiDung.MatKhau = txtMatKhau.Text;
                    nguoiDung.Email = txtEmail.Text;

                    nguoiDung.DienThoai = txtDienThoai.Text;
                    nguoiDung.SoCMND = txtCMND.Text;
                    nguoiDung.DiaChi = txtDiaChi.Text;
                    nguoiDung.MaKichHoat = int.Parse(txtMaKichHoat.Text);
                    nguoiDung.TinhTrangKichHoat = int.Parse(dropTinhTrang.SelectedValue.ToString());
                    nguoiDung.ThoiGianDangKy = DateTime.Parse(txtThoiGianDK.Text);
                    nguoiDung.ThoiGianHetHan = DateTime.Parse(txtThoiGianHetHan.Text);
                    nguoiDung.ViPham = false;
                    nguoiDung.Deleted = false;
                    danhsach.NGUOI_DUNGs.InsertOnSubmit(nguoiDung);
                    danhsach.SubmitChanges();
                    lblResult.Text = "Thêm tài khoản thành công";
                }
                else
                {
                    lblResult.Text = "Trùng tên tài khoản";
                }
            }
            catch (Exception ex)
            {
                lblResult.Text = "Thêm tài khoản thất bại";
            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            for (int i = 0; i < dsEmail.Count; i++)

                if (args.Value.ToString().Trim() == dsEmail[i])
                {
                    args.IsValid = false;
                }
        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            for (int i = 0; i < dsCMND.Count; i++)

                if (args.Value.ToString().Trim() == dsCMND[i])
                {
                    args.IsValid = false;
                }
        }

        protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
        {
            
            for (int i = 0; i < dsUserName.Count; i++)

                if (args.Value.ToString().Trim() == dsUserName[i])
                {
                    args.IsValid = false;
                }
        }

        protected void CustomValidator4_ServerValidate(object source, ServerValidateEventArgs args)
        {

            string s = args.Value.ToString();
            for (int i = 0; i < s.Length; i++)
                if ((s[i] == '/') || (s[i] == '-') || (s[i] == '?') || (s[i] == '<') || (s[i] == '>') || (s[i] == '"') || (s[i] == '+') || (s[i] == '!') || (s[i] == '~') || (s[i] == '`') || (s[i] == '#') || (s[i] == '$') || (s[i] == '%') || (s[i] == '^') || (s[i] == '&') || (s[i] == '*') || (s[i] == '(') || (s[i] == ')'))
                {
                    args.IsValid = false;
                }
        }


    }
}